<!--2019/1/2 15:29. by soft-->
<#assign title='评定记录'>
<#include '../header.ftl'>

<div class="data">
  <div class="layui-card">
    <div class="layui-card-header">
      <div class="pull-left">
      <@shiro.hasRole name="college-admin">
        <span class="layui-badge layui-bg-cyan">${_USER.college.name!'自己的学院'}</span>
      </@shiro.hasRole>
      </div>
      <#--搜索框-->
      <div class="pull-right">
        <select id="incident-select" class="form-select">
          <option value="">所有事件</option>
          <#list incidents as inc>
              <option value="${inc.id}">${inc.name}</option>
          </#list>
        </select>
        <@shiro.hasRole name="admin">
          <select id="college-select" class="form-select">
            <option value="">所有学院</option>
            <#list colleges as college>
                <option value="${college.id}">${college.name}</option>
            </#list>
          </select>
        </@shiro.hasRole>
        <select id="dept-select" class="form-select">
          <option value="">所有部门</option>
            <#list depts as dept>
              <option value="${dept.id}">${dept.name}</option>
            </#list>
        </select>
        <select id="item-select" class="form-select">
          <option value="">所有项目</option>
          <#if items??>
            <#list items as item>
            <option value="${item.id}">${item.name}</option>
            </#list>
          </#if>
        </select>
        <form id="search" style="display: inline-block;">
          <input class="form-input" id="username" placeholder="用户名">
          <button class="layui-btn layui-btn-sm" type="submit">搜索</button>
        </form>
      </div>
    </div>
    <div class="layui-card-body">
      <table id="data-table"></table>

      <script type="text/html" id="data-tool">
        {{# if(d.audit) { }}
          <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="reject_">驳回</a>
        {{# } else { }}
          <a class="layui-btn layui-btn-xs" lay-event="pass_">通过</a>
        {{# } }}
      </script>
    </div>
  </div>

  <script>
    var methods;
    layui.use(['layer', 'jquery', 'laytpl', 'form', 'table'], function() {
      var $ = layui.$;
      var layer = layui.layer;
      var table = layui.table;

      var $incSelect = $("#incident-select");
      var $deptSelect = $("#dept-select");
      var $itemSelect = $("#item-select");
      var $username = $('#username');
      var $college = $('#college-select');

      table.render({
        elem: '#data-table',
        url: '${root}/item/record/page',
        method: 'post',
        page: true,
        cols: [[
          {title: '序号', type: 'numbers'},
          {title: '用户', width: 80, templet: '<b><b class="layui-badge layui-bg-gray">{{d.user.name}}</b></b>'},
          {title: '所在部门', templet: '<b>[{{d.user.department.name}}]{{d.user.college.name}}</b>'},
          {field: 'name', title: '项目'},
          {title: '所属事件', templet: '<b><b class="layui-badge layui-bg-green">{{d.incident.name}}</b></b>'},
          {title: '级别', templet: '<b><b class="layui-badge layui-bg-cyan">{{d.rank.name}}</b></b>'},
          {title: '身份', width: 90, templet: '<b><b class="layui-badge layui-bg-blue">{{getPos(d.place.place)}}</b></b>'},
          {field: 'score', width: 60, title: '分值'},
          {title: '审核', width: 80, templet: "<i>{{d.audit?'<b class=\"layui-badge layui-bg-blue\">已审核</b>':'<b class=\"layui-badge\">未审核</b>'}}</i>"},
          {title: '立项时间', width: 105, templet: "<b>{{d.beginTime?d.beginTime:'<b class=\"layui-badge layui-bg-gray\">请忽略</b>'}}</b>"},
          {field: 'endTime', title: '结项时间', width: 105},
          {title: '操作', toolbar: '#data-tool', width: 80}
        ]],
        parseData: function (res) {
          return {
            code: 0,
            msg: res.message,
            count: (res.payload ? res.payload.totalElements : 0),
            data: (res.payload ? res.payload.content : 0)
          }
        }
      });

      methods = {
        pass_: function (record) {
          $.post('${root}/item/record/audit/1', {id: record.id}, function (data) {
            if (data.status === 200) {
              layer.msg('通过审核成功！', {icon: 6, time: 1200});
              table.reload('data-table');
            } else {
              layer.alert('审核失败：' + data.message, {title: '错误提示', icon: 5});
            }
          });
        },
        reject_: function (record) {
          $.post('${root}/item/record/audit/0', {id: record.id}, function (data) {
            if (data.status === 200) {
              layer.msg('驳回审核成功！', {icon: 6, time: 1200});
              table.reload('data-table');
            } else {
              layer.alert('驳回失败：' + data.message, {title: '错误提示', icon: 5});
            }
          });
        }
      };

      table.on('tool', function (o) {
        var mtd = methods[o.event];
        if (mtd != null) {
          mtd(o.data);
        }
      });

      $college.change(function (e) {
        var cgid = e.target.value;
        $.post('${root}/dept/list', {cgid: cgid}, function (data) {
          $deptSelect.html('<option value="">所有部门</option>');
          if (data.status === 200) {
            layui.each(data.payload, function (index, val) {
              $deptSelect.append('<option value="'+val.id+'">'+val.name+'</option>');
            });
          } else {
            layer.alert('该学院不存在部门！');
          }
          reloadTable(e);
        });
      });

      $incSelect.change(reloadTable);
      $deptSelect.change(reloadTable);
      $itemSelect.change(reloadTable);
      $('#search').submit(function (e) {
        e.preventDefault();
        /*if ($username.val().trim().length === 0) {
          return;
        }*/
        reloadTable(e);
      });

      function reloadTable(e) {
        table.reload('data-table', {where: getWhere()});
      }

      function getWhere() {
        var iid = $incSelect.val();
        var cid = $college.val();
        var did = $deptSelect.val();
        var itemId = $itemSelect.val();
        var username = $username.val().trim();
        return {username: username, itemId: itemId, incidentId: iid, collegeId: cid, deptId: did};
      }
    });
  </script>
</div>

<#include '../footer.ftl'>